﻿using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using Xc.DataAccess.Core.Properties;
namespace Xc.DataAccess.Core
{

    /// <summary>
    /// 设备工厂服务
    /// </summary>
    /// <typeparam name="TService"></typeparam>
    /// <typeparam name="TOption"></typeparam>
    public class DevcieFactoryService<TService, TOption> : IDevcieFactoryService<TService, TOption> where TService : IDeviceService<TOption> where TOption : class, IDevcieId
    {
        private readonly IServiceProvider serviceProvider;
        /// <summary>
        /// 设备存储列表
        /// </summary>

        private readonly ConcurrentDictionary<IDevcieId, TService> keyValuePairs = new ConcurrentDictionary<IDevcieId, TService>();
        /// <summary>
        /// 设备工厂服务
        /// </summary>
        /// <param name="serviceProvider"></param>
        public DevcieFactoryService(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        /// <summary>
        /// 设置服务
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task<ResultData<TOption, TService>[]> SetServiceAsync(params TOption[] options)
        {
            var results = options.Select(item => Task.Run(async () =>
             {
                 var service = serviceProvider.GetService<TService>();
                 if (service == null)
                 {
                     return ResultData<TOption, TService>.Reject($"{TextResource.not_found_any_service_in_service_bus}:{typeof(TService).Name}");
                 }
                 var result =await service.OpenAsync(item);
                 if (result.IsError)
                 {
                     return ResultData<TOption, TService>.Reject(result.Message);
                 }
                 return ResultData<TOption, TService>.Accept(result.Message, item, service);
             }));
            var res = await Task.WhenAll(results);
            return res;
        }
        /// <summary>
        /// 获取功能列表
        /// </summary>
        /// <returns></returns>
        public string[] GetFunctions() => keyValuePairs.Keys.Select(p => p.FunctionName).ToArray();
        /// <summary>
        /// 获取服务列表
        /// </summary>
        /// <returns></returns>
        public int[] GetServiceIndexs() => keyValuePairs.Keys.Select(p => p.ServiceIndex).ToArray();
        /// <summary>
        /// 获取服务
        /// </summary>
        /// <param name="devcieId"></param>
        /// <returns></returns>
        public TService GetService(IDevcieId devcieId)
        {
            if (keyValuePairs.ContainsKey(devcieId))
            {
                return keyValuePairs[devcieId];
            }
            else
            {
                return default;
            }
        }
        /// <summary>
        /// 根据服务序号获取服务
        /// </summary>
        /// <param name="serviceIndex"></param>
        /// <returns></returns>
        public TService GetService(int serviceIndex)
        {
            var key = keyValuePairs.FirstOrDefault(p => p.Key.ServiceIndex == serviceIndex);
            if (key.Key != null)
            {
                return GetService(key.Key);
            }
            else
            {
                return default;
            }
        }
        /// <summary>
        /// 获取服务
        /// </summary>
        /// <param name="functionName">功能名称</param>
        /// <returns></returns>
        public TService GetService(string functionName)
        {
            var key = keyValuePairs.FirstOrDefault(p => p.Key.FunctionName == functionName);
            if (key.Key != null)
            {
                return GetService(key.Key);
            }
            else
            {
                return default;
            }
        }
    }
}
